if (!global.ShadowMamba) {
global.ShadowMamba = {};
        }
var sm = global.ShadowMamba;

function sm_summonRandom(s) {
var unit = s.source_unit;
var tile = s.target_tile;
var skill = s.skill;
var item = s.item;
var flags;
if (skill)
flags = skill.ScriptFlags;
else if (item)
flags = item.ScriptFlags;
if (flags.get("summon_random")) {
if (flags.get("summon_rare_random")) {
if (!flags.get("summon_rare_random_chance")) {
Say("Error: You must use the summon rare random chance flag alongside the summon rare random flag!");
return;
}
if (flags.get("summon_rare_random_chance")) {
var rc = flags.get("summon_rare_random_chance");
var math = Math.random().toFixed(2);
if (math > rc) {
var random = slashString(flags.get("summon_random"));
target = shared.CreateUnit(random);
if (!tile.IsValidDestinationFor(target)) {
say("Error: You cannot summon there!");
return;
}
if (tile.IsValidDestinationFor(target)) {
shared.Map.AddUnit(target, tile.X, tile.Y);
target.Team = unit.Team;
if (flags.get("add_random_skills")) {
sm_addRandomSkills(s, target);
}
shared.ShareUnitUpdate(target);
var sMessage = (flags.get("summon_success_message"));
if (sMessage) {
var msg = chooseFromARandomSelectString(sMessage);
if (skill)
say((msg).format(unit.GetName(),target.GetName()));
else if (item)
say((msg).format(unit.GetName(),item.FriendlyName,target.GetName()));
}
var sSound = flags.get("summon_success_sound");
if (sSound) {
randomSound(sSound);
}
if (flags.get("apply_effects")) {
if (flags.get("apply_effects_chance")) {
var newEffects = chooseFromARandomSelectString(flags.get("apply_effects"));
var chance = flags.get("apply_effects_chance");
var math = Math.random().toFixed(2);
if (math > chance) {
}
else if (math <= chance) {
var effects = newEffects.split(",");
for (var i = 0; i < effects.length; i++) {
shared.ApplyEffect(effects[i], target, tile);
}
}
}
}
}
}
else if (math <= rc) {
var rareRandom = slashString(flags.get("summon_rare_random"));
target = shared.CreateUnit(rareRandom);
if (!tile.IsValidDestinationFor(target)) {
say("Error: You cannot summon there!");
return;
}
if (tile.IsValidDestinationFor(target)) {
shared.Map.AddUnit(target, tile.X, tile.Y);
target.Team = unit.Team;
if (flags.get("add_random_skills")) {
sm_addRandomSkills(s, target);
}
shared.ShareUnitUpdate(target);
var sMessage = (flags.get("summon_success_message"));
if (sMessage) {
var msg = chooseFromARandomSelectString(sMessage);
if (skill)
say((msg).format(unit.GetName(),target.GetName()));
else if (item)
say((msg).format(unit.GetName(),item.FriendlyName,target.GetName()));
}
var sSound = flags.get("summon_success_sound");
if (sSound) {
randomSound(sSound);
}
if (flags.get("apply_effects")) {
if (flags.get("apply_effects_chance")) {
var newEffects = chooseFromARandomSelectString(flags.get("apply_effects"));
var chance = flags.get("apply_effects_chance");
var math = Math.random().toFixed(2);
if (math > chance) {
}
else if (math <= chance) {
var effects = newEffects.split(",");
for (var i = 0; i < effects.length; i++) {
shared.ApplyEffect(effects[i], target, tile);
}
}
}
}
}
}
}
}
if (!flags.get("summon_rare_random")) {
var random = slashString(flags.get("summon_random"));
target = shared.CreateUnit(random);
if (!tile.IsValidDestinationFor(target)) {
say("Error: You cannot summon there!");
return;
}
if (tile.IsValidDestinationFor(target)) {
shared.Map.AddUnit(target, tile.X, tile.Y);
target.Team = unit.Team;
if (flags.get("add_random_skills")) {
sm_addRandomSkills(s, target);
}
shared.ShareUnitUpdate(target);
var sMessage = (flags.get("summon_success_message"));
if (sMessage) {
var msg = chooseFromARandomSelectString(sMessage);
if (skill)
say((msg).format(unit.GetName(),target.GetName()));
else if (item)
say((msg).format(unit.GetName(),item.FriendlyName,target.GetName()));
}
var sSound = flags.get("summon_success_sound");
if (sSound) {
randomSound(sSound);
}
if (flags.get("apply_effects")) {
if (flags.get("apply_effects_chance")) {
var newEffects = chooseFromARandomSelectString(flags.get("apply_effects"));
var chance = flags.get("apply_effects_chance");
var math = Math.random().toFixed(2);
if (math > chance) {
}
else if (math <= chance) {
var effects = newEffects.split(",");
for (var i = 0; i < effects.length; i++) {
shared.ApplyEffect(effects[i], target, tile);
}
}
}
}
}
}
}
}
